<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\admin\model\User;
use fast\Random;

/**
 * 修改登录密码、支付密码
 */
class Password extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 修改支付密码
     *
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $model = new User();
        $data = input('post.');
       // $admin_info = $model->where('id', $user_id)->find();
        if ($data) {
            if ($data['pay_password'] == $data['password_again']) {     //   输入交易密码加密+两次输入的密码比对
                $salt = Random::alnum();     //  加密盐
                $md5_password = md5(md5($data['pay_password']) . $salt);    //  给用户输入的密码加密
                unset($data['password_again']);   //  销毁确认密码
                $result = User::where('id', $user_id)->update(['pay_password' => $md5_password, 'pay_salt' => $salt]);
                if ($result) {
                    $this->success('修改成功啦!');
                } else {
                    $this->error('修改失败');
                }
            } else {
                $this->error('再次输入不一致!');
            }
        }
    }

    /**
     * 修改登录密码
     *
     */
    public function edit()
    {
        $user_id = $this->auth->id;
        $data = input('post.');
        $admin = new User();
        $admin_info = $admin->where('id', $user_id)->find();
        if ($data) {
            $aa = md5(md5($data['password_origin']) . $admin_info['salt']);       //  输入原密码加密
            if ($aa == $admin_info['password']) {            //  用户输入的和数据库的对比
                if ($data['password'] == $data['password_again']) {     //  两次输入的密码对比
                    $salt = Random::alnum();     //  加密盐
                    $md5_password = md5(md5($data['password']) . $salt);    //  给用户输入的密码加密
                    unset($data['password_origin']);   //  销毁原密码
                    unset($data['password_again']);   //  销毁确认密码
                    $result = User::where('id', $user_id)->update(['password' => $md5_password,'salt'=>$salt]);
                    if ($result) {
                        $this->success('修改成功啦!');
                    } else {
                        $this->error('修改失败');
                    }
                } else {
                    $this->error('再次输入不一致!');
                }
            } else {
                $this->error('原密码输入错误!');
            }
        }
    }


}
